// ==UserScript==
  // @name         上班倒计时
  // @namespace    http://tampermonkey.net/
  // @version      0.1
  // @description  try to take over the world!
  // @author       You
  // @match        https://tu.tujia.com/webPage/myAttendance/attendanceRecord?user=*
  // @icon         https://www.google.com/s2/favicons?sz=64&domain=tujia.com
  // @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js
  // @grant        none
  // @run-at document-end
  // ==/UserScript==

  (function () {
    "use strict";
    // 最外层元素
    let tableDiv = document.querySelector(".attendanceRecord-list");
    let tbody = tableDiv.querySelector("tbody");
    let createDiv = null;
    function doWrite() {
      let trArr = Array.from(
        tbody.querySelectorAll("tr")
      );
      let trArrLen = trArr.length;
      // 总工时数
      let totalWorkTime = 0;
      let isLastDayDone = true;
      let lastDayStartWorkTime = "";
      trArr.forEach((item, index) => {
        const workTime = +item.querySelector("td:nth-child(7) span").innerText;
        if (workTime >= 11.5) {
          // 加班了 标记红色
          item.querySelector("td:nth-child(7)").style =
            "text-align: center;background: red;";
        } else if (workTime < 9) {
          item.querySelector("td:nth-child(7)").style =
            "text-align: center;background: yellow;";
        }
        // 最后一项
        if (index === trArrLen - 1) {
          if (workTime >= 9) {
            totalWorkTime += workTime;
          } else {
            isLastDayDone = false;
          }
          lastDayStartWorkTime =
            item.querySelector("td:nth-child(5)").innerText;
        } else {
          totalWorkTime += workTime;
        }
      });
      let avgTime = 0;
      if (isLastDayDone) {
        avgTime = totalWorkTime / trArrLen;
      } else {
        avgTime = totalWorkTime / (trArrLen - 1);
      }
      avgTime = avgTime.toFixed(2);
      if (createDiv) {
        tableDiv.removeChild(createDiv)
      }
      createDiv = document.createElement("div")
      const divHtml = `
        <ul style="list-style: none;margin: 0; padding: 0;">
            <li style="margin-bottom: 8px">
                <span>平均工时：</span>
                <span>${avgTime}小时/天 ${
        isLastDayDone ? "" : "【未包含最后一天】"
      }</span>
            </li>
            <li style="margin-bottom: 8px">
                <span>下班时间：</span>
                <span>${moment(lastDayStartWorkTime).add(9, "h").format('HH:mm:ss')}</span>
            </li>
            <li style="margin-bottom: 8px">
                <span>距离下班：</span>
                <span id="endWork"></span>
            </li>
            <li style="margin-bottom: 8px">
                <span>加班时间：</span>
                <span>${moment(lastDayStartWorkTime).add(11.5, "h").format('HH:mm:ss')}</span>
            </li>
            <li style="margin-bottom: 8px">
                <span>加班下班：</span>
                <span id="addEndWork"></span>
            </li>
        </ul>
    `;
    createDiv.style = `
        width: 100%;
    `;
    createDiv.innerHTML = divHtml;
      tableDiv.insertBefore(createDiv, tableDiv.children[0]);
      const endWorkM = moment(lastDayStartWorkTime).add(9, "h");
      const endWork = document.querySelector("#endWork");
      const endAddWork = document.querySelector("#addEndWork");
      const endaddWorkTime = moment(lastDayStartWorkTime).add(11.5, "h");
      let doTimer = setInterval(() => {
        const now = moment();
        const endWorkH = endWorkM.diff(now, "h");
        const endWorkm = endWorkM.diff(now, "m");
        const endWorkS = endWorkM.diff(now, "s");
        const endAddWorkH = endaddWorkTime.diff(now, "h");
        const endAddWorkm = endaddWorkTime.diff(now, "m");
        const endAddWorkS = endaddWorkTime.diff(now, "s");
        if (endWorkS >= 0) {
          endWork.innerText = `${endWorkH}小时 ${
            endWorkm - endWorkH * 60
          }分钟 ${endWorkS - endWorkm * 60}秒`;
        } else if (endWorkH > -3) {
          endWork.innerText = `已加班${endWorkH ? -endWorkH : endWorkH}小时 ${
            -endWorkm - -endWorkH * 60
          }分钟 ${-endWorkS - -endWorkm * 60}秒`;
        } else {
          endWork.innerText = "要死了吧";
        }
        if (endAddWorkS >= 0) {
          endAddWork.innerText = `${endAddWorkH}小时 ${
            endAddWorkm - endAddWorkH * 60
          }分钟 ${endAddWorkS - endAddWorkm * 60}秒`;
        } else {
          endAddWork.innerText = "赶紧打车下班吧";
        }
        if (endWorkS < 0 && endAddWorkS < 0) {
          clearInterval(doTimer);
        }
      }, 1000);
    }

    // 创建一个观察器实例并传入回调函数
    const observer = new MutationObserver(() => {
      console.log("监听到变化了");
      doWrite();
    });

    observer.observe(tbody, { childList: true });
  })();